﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Reflection;

namespace wsErrorsLog
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {






        [WebMethod]
        public Result<string> RetrieveErrors(string jsonParameters)
        {
            return ProcessRequest<ErrorDetailsDTO[], Parameters>(
                jsonParameters,
                p => ErrorDetailsDTO.CalculateErrors(p.LastErrorIdSynced));

        }


        private static Result<string> ProcessRequest<U, V>(string jsonParameters, Func<V, U> func)
        {
            string response = null;
            try
            {
                V parameter = Newtonsoft.Json.JsonConvert.DeserializeObject<V>(jsonParameters);
                U result = func(parameter);
                response = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            }
            catch (Exception ex)
            {                
                return new Result<string>() { Value = null, ErrorType = 1, WebServiceError = ex.Message };
            }
            return new Result<string>() { Value = response };
        }
    }
}
